const bpm = {
    isReleased: false,
    isPercent: function (value) {
        return /^\d+.?\d*%$/g.test(value);
    },
    parseFloat: function (value) {
        var s = parseFloat(value);
        if (isNaN(s)) {
            return 0;
        }
        return s;
    },
    stopBubble: function (e) {
        e = e ? e : window.event;
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        else {
            e.cancelBubble = true();
        }
    },
    attachEvent: function (src, eventName, func) {
        if (src instanceof Array) {
            for (var i in src) {
                if (src[i].attachEvent) {
                    src[i].attachEvent("on" + eventName, func);
                }
                else {
                    src[i].addEventListener(eventName, func, false);
                }
            }
        }
        else {
            if (src.attachEvent) {
                src.attachEvent("on" + eventName, func);
            }
            else {
                src.addEventListener(eventName, func, false);
            }
        }
    },
    onMouseDown: function (event, shape, beginMove) {
        bpm.isReleased = false;
        bpm.attachEvent((beginMove ? shape : (shape.canvas || shape.ui.parentNode)), "mouseup", bpm.onMouseUp);
        document.onselectstart = function () { return false; };
        bpm.mousedowntimeout = setTimeout(function () {
            if (bpm.mousedowntimeout) {
                delete bpm.mousedowntimeout;
            }
            if (bpm.isReleased) {
                document.onselectstart = null;
                return;
            }
            beginMove ? beginMove(event) : shape.beginMove(event);
        }, 50);
        bpm.stopBubble(event);
    },
    //当鼠标松开时触发的事件
    onMouseUp: function (_) {
        if (bpm.mousedowntimeout) {
            clearTimeout(bpm.mousedowntimeout);
            delete bpm.mousedowntimeout;
        }
        document.onselectstart = null;
        bpm.isReleased = true;
        bpm.removeEvent(this, "mouseup", bpm.onMouseUp);
    },
    removeEvent: function (src, eventName, func) {
        if (src.detachEvent) {
            src.detachEvent("on" + eventName, func);
        }
        else {
            src.removeEventListener(eventName, func, false);
        }
    },
    createElement: function (tagName, attributes, styles, text, events) {
        var v = document.createElement(tagName);
        if (attributes) {
            for (var a in attributes) {
                if (attributes[a]) {
                    if (a == "className") {
                        v.className = attributes[a];
                    }
                    else {
                        v.setAttribute(a, attributes[a]);
                    }
                }
            }
        }
        if (styles) {
            for (var s in styles) {
                v.style[s] = styles[s];
            }
        }
        if (text != null && text != undefined) {
            v.appendChild(document.createTextNode(text));
        }
        if (events) {
            for (var event in events) {
                v[event] = events[event];
            }
        }
        return v;
    },
    preventDefault: function (e) {
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
    },
    remove: function (element) {
        element && element.parentNode.removeChild(element);
    },
    setText: function (element, text) {
        if (typeof (element) == "string") {
            element = document.getElementById(element);
        }
        if (!element) {
            return;
        }
        element.innerHTML = "";
        element.appendChild(document.createTextNode(text));
    },
    clear: function (element) {
        for (var i = 0; i < element.childNodes.length; i++) {
            element.removeChild(element.childNodes[i]);
            i--;
        }
    }
};

bpm.Dialog = {
    onMouseMove: function (event) {
        var left = parseFloat(bpm.adorner.ui.style.left.replace("px", "")) + event.clientX - bpm.adorner.initX;
        if (left < 0) {
            left = 0;
        }
        else if ((left + bpm.adorner.ui.offsetWidth) > this.ownerDocument.documentElement.clientWidth) {
            left = this.ownerDocument.documentElement.clientWidth - bpm.adorner.ui.offsetWidth;
        }
        bpm.adorner.ui.style.left = left + "px";
        var top = parseFloat(bpm.adorner.ui.style.top.replace("px", "")) + event.clientY - bpm.adorner.initY;
        if (top < 0) {
            top = 0;
        }
        else if ((top + bpm.adorner.ui.offsetHeight) > this.ownerDocument.documentElement.clientHeight) {
            top = this.ownerDocument.documentElement.clientHeight - bpm.adorner.ui.offsetHeight;
        }
        bpm.adorner.ui.style.top = top + "px";
        bpm.adorner.initX = event.clientX;
        bpm.adorner.initY = event.clientY;
        bpm.preventDefault(event);
    },
    onMouseUpOrLeave: function (event) {
        if (!bpm.adorner) {
            return;
        }
        document.onselectstart = null;
        if (bpm.adorner.mask) {
            bpm.remove(bpm.adorner.mask);
        }
        delete bpm.adorner;
        bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mousemove", bpm.Dialog.onMouseMove);
        bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mouseup", bpm.Dialog.onMouseUpOrLeave);
        bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mouseout", bpm.Dialog.onMouseUpOrLeave);
    },
    showMask: function (_window) {
        var div = _window.document.getElementById("yc_div_mask");
        if (!div) {
            div = bpm.createElement("div", { id: "yc_div_mask" }, { zIndex: 1000, position: "absolute", top: "0px", width: "100%", left: "0px", height: "100%", backgroundColor: "#92CDDE", opacity: 0.2 });
            _window.document.body.appendChild(div);
            div.style.display = "";
            bpm.attachEvent(div, "mouseout", function (ev) {
                bpm.stopBubble(ev);
            });
        }
        else if (div.style.display == "") {
            div.style.zIndex = parseInt(div.style.zIndex) + 2;
        }
        else {
            div.style.display = "";
        }
        bpm.Dialog.addCounter(_window);
    },
    hideMask: function (_window) {
        var div = _window.document.getElementById("yc_div_mask");
        if (div) {
            if (bpm.Dialog.getCounter(_window) == 1) {
                div.style.display = "none";
            }
            else {
                div.style.zIndex = parseInt(div.style.zIndex) - 2;
            }
            bpm.Dialog.minusCounter(_window);
        }
    },
    addCounter: function (_window) {
        if (!_window._ychart_counter) {
            _window._ychart_counter = 1;
        }
        else {
            _window._ychart_counter++;
        }
    },
    minusCounter: function (_window) {
        if (!_window._ychart_counter) {
            return;
        }
        _window._ychart_counter--;
    },
    getCounter: function (_window) {
        if (!_window._ychart_counter) {
            return 0;
        }
        return _window._ychart_counter;
    }
}

bpm.confirm = function (title, message, callback) {
    var _parent = window; //.top
    var mb = _parent.document.getElementById("yc_messageboxconfirm");
    if (!mb) {
        mb = bpm.createElement("div", { id: "yc_messageboxconfirm" }, { height: "150px", width: "280px", zIndex: 2000, position: "absolute", display: "none" });
        _parent.document.body.appendChild(mb);
    }
    if (!message) {
        message = title;
        title = "提示";
    }
    if (mb.style.display != "") {
        bpm.Dialog.showMask(_parent);
        mb.style.display = "";
        var mb_border = bpm.createElement("div", { className: "bpm-confirm-bg" });
        mb.appendChild(mb_border);
        var mb_content = bpm.createElement("div", { className: "bpm-confirm-content" });
        mb.appendChild(mb_content);
        var mb_title = bpm.createElement("div", { className: "bpm-confirm-title" }, {});
        mb_content.appendChild(mb_title);
        var mb_title_1 = bpm.createElement("div", null, { float: "left", textIndent: "10px", color: "#FFFFFF", cursor: "default" });
        mb_title.appendChild(mb_title_1);
        mb_title_1.appendChild(document.createTextNode(title));
        var adorner = { x: 0, y: 0, x1: 0, y1: 0 };
        bpm.attachEvent(mb_title, "mousedown", function (event) {
            bpm.onMouseDown(event, {
                ui: mb,
                beginMove: function (event) {
                    bpm.attachEvent((event.target || event.srcElement).ownerDocument.body, "mousemove", onmousemove);
                    bpm.attachEvent((event.target || event.srcElement).ownerDocument.body, "mouseup", onmouseuporleave);
                    bpm.attachEvent((event.target || event.srcElement).ownerDocument.body, "mouseleave", onmouseuporleave);
                    adorner.x = event.clientX;
                    adorner.y = event.clientY;
                    adorner.x1 = mb.offsetLeft;
                    adorner.y1 = mb.offsetTop;
                }
            });
        });
        var onmousemove = function (event) {
            var temp = event.clientX - adorner.x + adorner.x1;
            if (temp < 0) {
                temp = 0;
            }
            else if (temp > ((event.target || event.srcElement).ownerDocument.documentElement.clientWidth - mb.offsetWidth)) {
                temp = event.screenWidth - mb.offsetWidth;
            }
            mb.style.left = temp + "px";
            temp = event.clientY - adorner.y + adorner.y1;
            if (temp < 0) {
                temp = 0;
            }
            else if (temp > ((event.target || event.srcElement).ownerDocument.documentElement.clientHeight - mb.offsetHeight)) {
                temp = event.screenHeight - mb.offsetHeight;
            }
            mb.style.top = temp + "px";
        }
        var onmouseuporleave = function (event) {
            bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mousemove", onmousemove);
            bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mouseup", onmouseuporleave);
            bpm.removeEvent((event.target || event.srcElement).ownerDocument.body, "mouseleave", onmouseuporleave);
        }
        var mb_title_2 = bpm.createElement("i", { className: "el-icon bpm-confirm-close" });
        mb_title_2.innerHTML = "<svg viewBox=\"0 0 1024 1024\" xmlns=\"http://www.w3.org/2000/svg\"><path fill=\"currentColor\" d=\"M764.288 214.592 512 466.88 259.712 214.592a31.936 31.936 0 0 0-45.12 45.12L466.752 512 214.528 764.224a31.936 31.936 0 1 0 45.12 45.184L512 557.184l252.288 252.288a31.936 31.936 0 0 0 45.12-45.12L557.12 512.064l252.288-252.352a31.936 31.936 0 1 0-45.12-45.184z\"></path></svg>";
        mb_title_2.onclick = function () {
            if (callback) {
                callback(false, "close");
            }
            this.parentNode.parentNode.parentNode.style.display = "none";
            bpm.Dialog.hideMask(this.ownerDocument.defaultView);
            bpm.clear(this.parentNode.parentNode.parentNode);
        };
        mb_title.appendChild(mb_title_2);
        var msg = bpm.createElement("div", null, { border: "1px solid #F5F5F5", width: "268px", height: "110px", backgroundColor: "#FFFFFF" });
        mb_content.appendChild(msg);
        var c11 = bpm.createElement("div", null, { width: "248px", textAlign: "left", height: "50px", verticalAlign: "middle", float: "left", color: "#444444", padding: "12px 10px 10px 10px", fontSize: "14px", cursor: "default" });
        msg.appendChild(c11);
        c11.appendChild(document.createTextNode(message));
        c11 = bpm.createElement("div", null, { margin: "auto", width: "150px" });
        msg.appendChild(c11);
        var btn = bpm.createElement("div", { className: "bpm-confirm-btn" });
        bpm.setText(btn, "确定");
        c11.appendChild(btn);
        btn.onclick = function () {
            if (callback) {
                callback(true);
            }
            this.parentNode.parentNode.parentNode.parentNode.style.display = "none";
            bpm.Dialog.hideMask(this.ownerDocument.defaultView);
            bpm.clear(this.parentNode.parentNode.parentNode.parentNode);
        }
        btn = bpm.createElement("div", { className: "bpm-confirm-btn" }, { marginLeft: "10px" });
        bpm.setText(btn, "取消");
        btn.onclick = function () {
            if (callback) {
                callback(false);
            }
            this.parentNode.parentNode.parentNode.parentNode.style.display = "none";
            bpm.Dialog.hideMask(this.ownerDocument.defaultView);
            bpm.clear(this.parentNode.parentNode.parentNode.parentNode);
        }
        c11.appendChild(btn);
        var onKeyDown = function (event) {
            if (event.keyCode == 27) {
                mb_title_2.click();
                bpm.removeEvent(_parent.document, "keydown", onKeyDown);
            }
        }
        bpm.attachEvent(_parent.document, "keydown", onKeyDown);
    }
    else {
        bpm.setText(mb.childNodes[1].childNodes[0].childNodes[0], title);
        bpm.setText(mb.childNodes[1].childNodes[1].childNodes[0], message);
    }
    mb.style.left = ((_parent.document.documentElement.clientWidth - mb.offsetWidth) / 2) + "px";
    mb.style.top = ((_parent.document.documentElement.clientHeight - mb.offsetHeight) / 2.6) + "px";
}
export default bpm;